package cn.com.inossem.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.foxykeep.datadroid.requestmanager.Request;

import java.util.List;

import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.EducationInfoBean;
import cn.com.inossem.model.SkillsInfoBean;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;

/**
 * 检索向导
 * <p>
 * <p>
 * detailed comment
 *
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class WebviewActivity extends RequestActivity {

	WebView webview;
	String id;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		id = getIntent().getStringExtra("id");
		// setContentView(R.layout.main_tab_layout);
		initView();

		launchRequest(WooRequestFactory.getWebview());

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {

		ImageView back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		ImageView shaerall = (ImageView) this.findViewById(R.id.shaerall);
		shaerall.setOnClickListener(this);

		webview = (WebView) findViewById(R.id.webview);

		webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webview.getSettings().setJavaScriptEnabled(true);

		// 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
		webview.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				// 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
				view.loadUrl(url);
				return true;
			}
		});

	}

	@Override
	public Request getInitialRequest() {
		return null;
	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.containsKey(WooRequestFactory.BUNDLE_EXTRA_WEBVIEW)) {

			String str = bundle
					.getString(WooRequestFactory.BUNDLE_EXTRA_WEBVIEW);

			List<SkillsInfoBean> skill_list = Utils.messageInfo.userInfo
					.getUserSkill();

			String skills = "";
			if (skill_list.size() > 0) {
				skills = selectData_info("t_dictionary", "id = "
						+ skill_list.get(0).getSkillID());
				System.out.println("skills:" + skill_list.get(0).getSkillID());
			}
			for (int i = 1; i < skill_list.size(); i++) {
				skills += ","
						+ selectData_info("t_dictionary", "id = "
						+ skill_list.get(i).getSkillID());
				System.out.println(skills);
			}

			List<EducationInfoBean> userEducation = Utils.messageInfo.userInfo
					.getUserEducation();

			String strEducation = "";
			if (userEducation.size() > 0) {
				strEducation = userEducation.get(0).getProfessional();
			}

			System.out.println("教育："
					+ Utils.messageInfo.userInfo.getStrUserEducation());
			// WebView加载web资源
			webview.loadUrl("http://zhirenapp.inossem.com/SapCareer/html5/index.html?piaData="
					+ str
					+ "&name="
					+ Utils.messageInfo.userInfo.getTruename()
					+ "&userId="
					+ Utils.messageInfo.userInfo.getId()
					+ "&headerUrl="
					+ WSConfig.IMAGE_URL
					+ "header/"
					+ Utils.messageInfo.userInfo.getImage()
					+ "&major="
					+ strEducation
					+ "&sapYear="
					+ Utils.messageInfo.userInfo.getSapYear()
					+ "&skills= " + skills
					+ "");
			System.out
					.println("dizhi:"
							+ "http://zhirenapp.inossem.com/SapCareer/html5/index.html?piaData="
							+ str + "&name="
							+ Utils.messageInfo.userInfo.getTruename()
							+ "&userId=" + Utils.messageInfo.userInfo.getId()
							+ "&headerUrl=" + WSConfig.IMAGE_URL + "header/"
							+ Utils.messageInfo.userInfo.getImage() + "&major="
							+ "" + "&sapYear="
							+ Utils.messageInfo.userInfo.getSapYear()
							+ "&skills= " + skills);
		} else {

		}
	}

	private String selectData_info(String tabname, String selection) {

		String name = "";

		DatabaseHelper dh = new DatabaseHelper(this);
		SQLiteDatabase db = dh.getReadableDatabase();
		Cursor cursor = db.query(tabname, null, selection, null, null, null,
				null);
		if (cursor.moveToFirst()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			name = cursor.getString(cursor.getColumnIndex("name_cn"));
		}
		cursor.close();
		db.close();
		dh.close();
		return name;
	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.webview_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.back:
				finish();
				break;
			case R.id.shaerall:
				Intent intent = new Intent(this, CareerShareDialogActivity.class);
				intent.putExtra("name", "【" + Utils.messageInfo.userInfo.getTruename() + "】的咨询职业生涯");
				intent.putExtra("url", webview.getUrl());
				startActivity(intent);
				break;
			default:
				break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
			case 1:
				setResult(1);
				finish();
			default:
				break;
		}
	}
}
